package com.zmzncs.lmtc.module.system.controller;

import com.zmzncs.lmtc.common.pojo.Rest;
import com.zmzncs.lmtc.common.util.JsonUtil;
import com.zmzncs.lmtc.common.util.RestUtil;
import com.zmzncs.lmtc.module.system.pojo.role.*;
import com.zmzncs.lmtc.module.system.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

@Api(tags = "角色")
@RestController
@RequestMapping(value="/system/role")
@Validated
public class RoleController {
	
	@Autowired
    private RoleService roleService;

	@ApiOperation(value = "查询角色列表")
	@GetMapping(value = "list")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
	})
	public Rest<List<RoleVO>> list() {
		List<RoleDTO> roleDTOList = roleService.getRoleList();
		List<RoleVO> roleVOList = JsonUtil.listTransform(roleDTOList, RoleVO.class);

		return RestUtil.success(roleVOList);
	}

	@ApiOperation(value = "添加角色")
	@PostMapping(value = "add")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
	})
	public Rest add(@RequestBody @Valid RoleAddForm form){
		roleService.addRole(form);

		return RestUtil.success();
	}

	@ApiOperation(value = "根据角色id查询角色")
	@GetMapping(value = "get-by-id")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true, dataType = "String"),
	})
	public Rest<RoleVO> getById(Long id) {
		RoleDTO roleDTO = roleService.getRoleById(id);
		RoleVO roleVO = new RoleVO();
		BeanUtils.copyProperties(roleDTO, roleVO);

		return RestUtil.success(roleVO);
	}
	
	@ApiOperation(value = "修改角色")
	@PostMapping(value = "update")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true, dataType = "String"),
	})
	public Rest update(@RequestBody @Valid RoleUpdateForm form) {
		roleService.updateRole(form);

		return RestUtil.success();
	}
	
	@ApiOperation(value = "删除角色")
	@PostMapping(value = "delete/{id}")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "header", name = "token", value = "请求头token", required = true),
			@ApiImplicitParam(name = "id", value = "角色id", required = true),
	})
	public Rest delete(@PathVariable Long id) {
		roleService.deleteRole(id);

		return RestUtil.success();
	}
	
}
